<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Deals
 *
 * @author ketu
 */
class Zhishidi_Question_Model_Mysql4_Question extends Mage_Core_Model_Mysql4_Abstract {

    protected function _construct() {
        $this->_init('zhishidi_question/question', 'question_id');
    }



    /*
    protected function _beforeSave(Mage_Core_Model_Abstract $object) {


        $dateFormatIso = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
        if (!$object->getDeal_from()) {
            $object->setDeal_from(Mage::getSingleton('core/date')->gmtDate());
        } else {
            $object->setDeal_from(Mage::app()->getLocale()->date($object->getDeal_from(), $dateFormatIso));
            $object->setDeal_from($object->getDeal_from()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
            $object->setDeal_from(Mage::getSingleton('core/date')->gmtDate(null, $object->getDeal_from()));
        }
        if (!$object->getDeal_to()) {
            $object->setDeal_to();
        } else {
            $object->setDeal_to(Mage::app()->getLocale()->date($object->getDeal_to(), $dateFormatIso));
            $object->setDeal_to($object->getDeal_to()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
            $object->setDeal_to(Mage::getSingleton('core/date')->gmtDate(null, $object->getDeal_to()));
        }

        return $this;
    }

    protected function _afterSave(Mage_Core_Model_Abstract $object) {
        $condition = $this->_getWriteAdapter()->quoteInto('deal_id = ?', $object->getId());
        $this->_getWriteAdapter()->delete($this->getTable('zhishidi_dailydeals/stores'), $condition);

        if (!$object->getData('stores')) {
            $object->setData('stores', $object->getData('store_id'));
        }
        if (in_array(0, $object->getData('stores'))) {
            $object->setData('stores', array(0));
        }

        foreach ((array) $object->getData('stores') as $store) {
            $storeArray = array();
            $storeArray['deal_id'] = $object->getId();
            $storeArray['store_id'] = $store;
            $this->_getWriteAdapter()->insert($this->getTable('zhishidi_dailydeals/stores'), $storeArray);
        }

        parent::_afterSave($object);
    }

    protected function _afterLoad(Mage_Core_Model_Abstract $object) {
        $select = $this->_getReadAdapter()->select()
                        ->from($this->getTable('zhishidi_dailydeals/stores'))
                        ->where('deal_id = ?', $object->getId());

        if ($data = $this->_getReadAdapter()->fetchAll($select)) {
            $storesArray = array();
            foreach ($data as $row) {
                $storesArray[] = $row['store_id'];
            }
            $object->setData('store_id', $storesArray);
        }
        return parent::_afterLoad($object);
    }

    protected function _beforeDelete(Mage_Core_Model_Abstract $object) {
        $adapter = $this->_getReadAdapter();
        $adapter->delete($this->getTable('zhishidi_dailydeals/stores'), 'deal_id=' . $object->getId());
    }
     *
     * 
     */

}

?>
